﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    public class FlyBat : GameScreen
    {
        ContentManager content;

        Texture2D backgroundTexture;
        Random rand = new Random(Int32.Parse(DateTime.Now.ToString("HHmmss")));

        List<Bat> batList = new List<Bat>();
        QuestionBat questionBat;

        int answer;
        int count=0, maxCount, correctCount=0;

        public FlyBat()
        {
            maxCount = 5;
        }

        public override void LoadContent(ContentManager content)
        {
            this.content = content;
            backgroundTexture = content.Load<Texture2D>("Texture/blank");
            LoadBat();
        }

        public void LoadBat()
        {
            GenerateBat();

            foreach (Bat b in batList)
            {
                b.LoadContent(content);
            }

            questionBat = new QuestionBat(answer, batList[0].BatTexture.Width, batList[0].BatTexture.Height);
            screenManager.AddAnimation(questionBat);
        }

        public override void Update(Touch touch, GameTime gameTime)
        {
            if (maxCount == count)
            {
                screenManager.RemoveScreen(this);
                screenManager.RemoveAnimation(questionBat);
                screenManager.AddScreen(new resultScreen(correctCount, maxCount));
            }

            foreach (GestureSample gesture in touch.Gestures)
            {
                Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);

                if (gesture.GestureType == GestureType.Tap)
                {
                    foreach (Bat b in batList)
                    {
                        if (tapLocation.X > b.Position.X &&
                            tapLocation.X < b.Position.X + b.BatTexture.Width &&
                            tapLocation.Y > b.Position.Y &&
                            tapLocation.Y < b.Position.Y + b.BatTexture.Height)
                        {
                            if (b.Correct)
                            {
                                screenManager.AddScreen(new Correct());
                                screenManager.RemoveAnimation(questionBat);
                                correctCount++;
                                LoadBat();
                            }
                            else
                            {
                                screenManager.AddScreen(new Incorrect());
                            }
                            count++;
                            return;
                        }
                    }
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);

            foreach (Bat b in batList)
            {
                b.Draw(spriteBatch);
            }
        }

        public void GenerateBat()
        {
            batList.Clear();

            int size = 4;

            answer = rand.Next(1, size+1);

            for (int i = 0; i < size; i++)
            {
                int save = i;
                int num = rand.Next(1, size+1);

                foreach (Bat b in batList)
                {
                    if (b.Type == num)
                    {
                        i--;
                        break;
                    }
                }
                if (save == i)
                {
                    if(num == answer)
                        batList.Add(new Bat(num, i + 1, true));
                    else
                        batList.Add(new Bat(num, i + 1, false));
                }
            }
        }
    }
}
